package common

import (
	"fmt"
	"strconv"
	"strings"

	pbledger "hundsun.com/hsl/hschain/protos/ledger"
)

var (
	tempBlockKey         = "temp-blk:"
	tempBlockEntryKey    = "temp-blkentry:"
	tempExecutedBlockKey = "temp-execblk:"
)

// GenTempBlockEntryKeyByHeight 根据区块高度生成临时区块标记存储Key
func GenTempBlockEntryKeyByHeight(height uint64) string {
	return fmt.Sprintf("%s%012d", tempBlockEntryKey, height)
}

// GenTempBlockKeyByHeight 根据区块高度生成临时区块存储Key
func GenTempBlockKeyByHeight(height uint64) string {
	return fmt.Sprintf("%s%012d", tempBlockKey, height)
}

// GetHeightFromTempBlockKey 根据key值获取区块高度
func GetHeightFromTempBlockKey(tempBlkKey string) uint64 {
	heightStr := strings.Split(tempBlkKey, ":")[1]
	height, err := strconv.Atoi(heightStr)
	if err != nil {
		return 0
	}
	return uint64(height)
}

// GenTempExecutedBlockKeyByHeight 根据高度生成临时执行后区块key
func GenTempExecutedBlockKeyByHeight(height uint64) string {
	return fmt.Sprintf("%s%012d", tempExecutedBlockKey, height)
}

// BlockEntry 区块标记，Checked为false表示待验证，为true表示验证通过准备写入存储
type BlockEntry struct {
	Block   *pbledger.Block
	Checked bool
}
